# coding=utf-8
import urllib.request
import http.client
import json
import config
import ssl


class BmobHelper(object):
    def __init__(self):
        self.requrl = "/1/classes/record_diff"  # 替换为自己的表名
        self.headerdata = {"X-Bmob-Application-Id": "9a609958fd900624df94cd0877a5c257", 
                            "X-Bmob-REST-API-Key": "4770800c725db21124398360ea63a408", 
                            "Content-Type": "application/json"}

    def record_op(self, datetime, diffOpenInPercent, diffCloseInPercent):
        context = ssl._create_unverified_context()
        conn = http.client.HTTPSConnection("api.bmob.cn", context=context, timeout=10)
        data = {'datetime': datetime, 'diffOpenInPercent': diffOpenInPercent, 'diffCloseInPercent': diffCloseInPercent};
        data = json.dumps(data)
        print("op_record: %s %s %s" % (datetime, diffOpenInPercent, diffCloseInPercent))
        try:
            conn.request(method="POST", url=self.requrl, body=data, headers=self.headerdata)
            response = conn.getresponse()
            data = response.read().decode('utf-8')
            print(data)
        except Exception as e:
            print(e)
            print("Add data error!")

        conn.close()
